package com.huellas.servlet.cita;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.huellas.excepcion.DAOExcepcion;
import com.huellas.negocio.GestionDoctor;
import com.huellas.negocio.GestionPacientes;
import com.huellas.negocio.GestionVacuna;
import com.huellas.negocio.GestionVacunaCita;

/**
 * Servlet implementation class CrearCitaVacunaServlet
 */
@WebServlet("/CrearCitaVacunaServlet")
public class CrearCitaVacunaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CrearCitaVacunaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		int CoCita = Integer.parseInt(request.getParameter("CoCita")); 
		String FeCita = request.getParameter("FeCita"); 
		String FeHora = request.getParameter("FeHora") + ":00";
		int CoCliente = Integer.parseInt(request.getParameter("CoCliente"));
		String NoPaciente = request.getParameter("NoPaciente");
		String NoDoctor = request.getParameter("NoDoctor");
		String NoVacuna = request.getParameter("NoVacuna");
		
		String FlTipoCita = "vacuna";
		int CoEstadoCita = 1;
		int CoPaciente = 0;
		int CoDoctor = 0;
		int CoVacuna = 0;
		
		try{
			
			GestionPacientes gestionPaciente = new GestionPacientes();
			CoPaciente = gestionPaciente.obtenerCoPaciente(NoPaciente, CoCliente);
			
			GestionDoctor gestionDoctor = new GestionDoctor();
			CoDoctor = gestionDoctor.obtenerCoDoctor(NoDoctor);
		
			GestionVacuna gestionVacuna = new GestionVacuna();
			CoVacuna = gestionVacuna.obtenerCoVacuna(NoVacuna);
			
			GestionVacunaCita gestionVacunaCita = new GestionVacunaCita();
			
			if (gestionVacunaCita.validarRegistroVacuna(CoVacuna, FeCita, FeHora, FlTipoCita, CoEstadoCita, CoPaciente, CoDoctor) == true){
				gestionVacunaCita.insertarVacunaCita(CoCita, FeCita, FeHora, FlTipoCita, CoEstadoCita, CoPaciente, CoDoctor, CoVacuna);
				
				RequestDispatcher requestDispatcher = request.getRequestDispatcher("confirmacion.jsp");
				requestDispatcher.forward(request, response);
			}else{
				RequestDispatcher requestDispatcher = request.getRequestDispatcher("duplicado.jsp");
				requestDispatcher.forward(request, response);
			}
		}catch (DAOExcepcion e){
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("error.jsp");
			requestDispatcher.forward(request, response);
		}
	}

}
